package com.javabean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.DataBase;

public class FindTotal {
	Connection con = DataBase.getConnection();
	PreparedStatement pre;
	ArrayList a = new ArrayList();
	ArrayList borrow = new ArrayList();
	ResultSet rs;

	public String getUserName(int id) {
		String sql = "select UserName from user where UserId=?";
		String username = "暂无描述";
		try {
			PreparedStatement pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			ResultSet rs = pre.executeQuery();
			if (rs.next()) {
				username = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return username;

	}

	public ArrayList find(int id) {
		System.out.print("id:"+id);
		String sql = "select* from device where did=?";
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			rs = pre.executeQuery();
			while (rs.next()) {
				if (rs.getString(2) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(2));
				}
				if (rs.getString(3) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(3));
				}
				if (rs.getString(4) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(4));
				}
				if (rs.getString(6) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(6));
				}

				if (rs.getString(7) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(7));
				}
				if (rs.getString(5) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(5));
				}

				if (rs.getString(8) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(8));
				}

				if (rs.getString(9) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(9));
				}
				if (rs.getString(10) == null) {
					a.add("暂无描述");
				} else {
					a.add(rs.getString(10));
				}

				

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;

	}

	public ArrayList findBorrow(int id) {
		String sql = "select * from borrow_record where DeviceId=?";
		try {
			pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			rs = pre.executeQuery();

			while (rs.next()) {
				if (rs.getString(3) == null) {
					borrow.add("暂无描述");
				} else {
					borrow.add(rs.getString(3));
				}
				if (rs.getString(5) == null) {
					borrow.add("暂无描述");
				} else {
					borrow.add(rs.getString(5));
				}

				if (rs.getString(6) == null) {
					borrow.add("暂无描述");
				} else {
					borrow.add(rs.getString(6));
				}
				String username = getUserName(rs.getInt(4));
				borrow.add(username);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return borrow;

	}

	

	
}
